package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.ForresterBeans;

import com.itboye.textileexpes.presenter.CategoryPresenter;
import com.itboye.textileexpes.presenter.MainCategoryPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.myset.ActivityCustomerService;
import com.itboye.textileexpres.widget.PagerSlidingTabStrip;
import com.itboye.textillexepres.db.PropSub;
import com.itboye.textillexepres.db.SearchDBHelper;
import com.itboye.textillexepres.db.SearchDBManager;

public class ActivityTypeTwoGoods extends BaseActivity implements
		OnClickListener, Observer {
	ImageView back;
	EditText key_word;
	String[] titles;
	ViewPager pager;
	PagerSlidingTabStrip tabs;
	ImageView carmar_img;
	MainCategoryPresenter presenter;
	ForresterBeans beans = new ForresterBeans();
	protected List<ForresterBeans> arrayListForm = new ArrayList<ForresterBeans>();
	protected List<String> CateID = new ArrayList<String>();
	SearchDBManager dbManager;
	SQLiteDatabase db;
	SearchDBHelper dbHelper;
	
	ImageView img_other,type_img;
	
	TextView goods_num,title;
	protected List<PropSub> propSub = new ArrayList<PropSub>();

	@Override
	public int initView() {
		// TODO Auto-generated method stub h1218
		return R.layout.activity_goods_search;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		dbManager = new SearchDBManager(this);
		tabs.setFillViewport(false);
		title.setText(getString(R.string.h1218));
		type_img.setVisibility(View.GONE);
		img_other.setVisibility(View.GONE);
		presenter = new MainCategoryPresenter(this);
		presenter.getInfo(beans);
		dbHelper = new SearchDBHelper(ActivityTypeTwoGoods.this);
		db = dbHelper.getWritableDatabase();
		db.execSQL("DELETE FROM " + SearchDBHelper.SEARCHDB_TABLENAME);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {
		case R.id.back:
			finish();
			db.execSQL("DELETE FROM " + SearchDBHelper.SEARCHDB_TABLENAME);
			break;
		case R.id.carmar_img:
			intent = new Intent(this, ActivityCarmarSearch.class);
			startActivity(intent);
			break;
		case R.id.key_word:
			intent = new Intent(this, ActivityTypeDetail.class);
			startActivity(intent);
			break;
		case R.id.img_other:
			Intent intent2=new Intent(ActivityTypeTwoGoods.this,ActivityCustomerService.class);
			startActivity(intent2);
			break;
		default:
			break;
		}
	}

	public class MyAdapter extends FragmentStatePagerAdapter {
		String[] _titles;

		public MyAdapter(FragmentManager fm, String[] titles) {
			super(fm);
			_titles = titles;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return _titles[position];
		}

		@Override
		public int getCount() {
			return _titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			Fragment bfrag2 = null;
			bfrag2 = new MeOfFragmentGoodsSearch(Integer.parseInt(CateID
					.get(position)), position);
			// bfrag2 = new FragmentGoodsSearch(position);
			return bfrag2;
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == MainCategoryPresenter.category_success) {
				arrayListForm = (List<ForresterBeans>) entity.getData();
				titles = new String[arrayListForm.size()];
				for (int i = 0; i < arrayListForm.size(); i++) {
					titles[i] = arrayListForm.get(i).getName();
					CateID.add(arrayListForm.get(i).getId());
					for (int j = 0; j < arrayListForm.size(); j++) {
						propSub.clear();
						propSub.addAll(arrayListForm.get(i).getSubProp());
						dbManager
								.add(arrayListForm.get(i).getSubProp(), i + "");
					}

				}
				pager.setAdapter(new MyAdapter(getSupportFragmentManager(),
						titles));
				tabs.setViewPager(pager);

			}
		}
	}
}
